<?php

function kebutuhan_jabatan(){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);
	
	$tgl = date("Y-m-d");
	$ftanggal = date('d-m-Y');
	if($_GET['tanggal']){ 
		list($ddtanggal , $mmtanggal , $yyyytanggal  ) = explode("-" ,$_GET['tanggal'] );
		$tgl = $yyyytanggal ."-". $mmtanggal."-".$ddtanggal;
		$ftanggal = $_GET['tanggal'];
	}
	$tanggal = array(
			'name'=>'tanggal',
			'id'=>'tanggal',
			'value'=>$ftanggal,
		);
	
	$view="<table border=\"0\">";
	$view .='
	<tr style="background-color:#010101;color:#fff;border:2px solid #fff;width:5%">
		<th style="background-color:010101;text-align:center;width:5%"  rowspan="2">#</th>
		<th style="background-color:010101;text-align:center;width:10%"  rowspan="2">No. Posisi</th>
		<th style="background-color:010101;text-align:center;width:35%"  rowspan="2">Jabatan</th>
		<th style="background-color:010101;text-align:center;width:20%;" colspan="4">Kebutuhan</th>
		<th style="background-color:010101;text-align:center;width:10%;" rowspan="2">Tersedia</th>
		<th style="background-color:010101;text-align:center;width:5%;" rowspan="2">PTK</th>
		<th style="background-color:010101;text-align:center;width:10%;" colspan="2"">Selisih</th>
		<th style="background-color:010101;text-align:center;width:5%;" rowspan="2">&nbsp;</th>
		'; 
	$view .='</tr>'."\n";
	$view .='
	<tr style="background-color:#010101;color:#fff;border:2px solid #fff;width:5%">
		<th style="text-align:center;">I</th>
		<th style="text-align:center;">II</th>
		<th style="text-align:center;">III</th>
		<th style="text-align:center;">Total Group</th>
		<th style="text-align:center;">+</th>
		<th style="text-align:center;">-</th>
		'; 
	$view .='</tr>'."\n";
		$query = "SELECT a.* 
			FROM jabatan a	
			INNER JOIN sk_jabatan b ON b.sk_id=a.sk_id
			WHERE b.active='Y' 
			AND a.jabatan_id NOT IN (select jabatan_id FROM jabatan_terkait_link)  
			ORDER BY a.urut
		" ;
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&task={$_GET['task']}&field={$_GET['field']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array();  
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$i++;
		$bgrows = ( ($i%2) == 0 ) ? '#DEDEDE' : '#EFEFEF' ;
			
		$jabatan = my_get_data_by_id('jabatan','jabatan_id' , $ey['jabatan_id']); 
		$nama_jabatan=get_jabatan_link_by_jabatan_id($ey['jabatan_id']) ;
		$nomor_posisi=get_nomor_posisi_link_by_jabatan_id($ey['jabatan_id']) ;
		$datas = explode("," , $ey['kebutuhan_tenaga_kerja']);
		$totalgroup=$datas[0]+$datas[1]+($datas[2]*2);
		$tersedia = getshifttersedia( $ey['jabatan_id'] ,$tgl );
		$proses_ptk = getproses_ptk( $ey['jabatan_id'] ,$tgl );
		$selisih_plus = ($tersedia+$proses_ptk > $totalgroup) ? ($tersedia+$proses_ptk - $totalgroup) :'-';
		$selisih_minus = ($tersedia+$proses_ptk < $totalgroup) ? ($totalgroup-$tersedia-$proses_ptk) :'-';

		$editproperty = array(
						'href'=>'index.php?com=seleksi_internal&task=edit_detail&sid='.$ey['seleksi_internal_detail_id'].'&id='.$seleksi_internal_id,
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$createproperty = array(
						'href'=>'index.php?com=kebutuhan&task=ptk_create&id='.$ey['jabatan_id'].'&jumlah='.$selisih_minus,
						'title'=>'Buat PTK'
				);
		$pr_button = button_icon( 'b_bookmark.png' , $createproperty  );		
		$button_create = ($selisih_minus <> '-' ) ? $pr_button: '&nbsp;' ;
		$view .='
		<tr style="background-color:'.$bgrows.'">
			<td align="center">'.$i.'</td>
			<td>'.$nomor_posisi.'</td>
			<td>'.$nama_jabatan.'</td>
			<td align="center">'.$datas[0].'</td>
			<td align="center">'.$datas[1].'</td>
			<td align="center">'.$datas[2].'</td>
			<td align="center">'.$totalgroup.'</td>
			<td align="center">'.$tersedia.'</td>
			<td align="center">'.$proses_ptk.'</td>
			<td align="center">'.$selisih_plus .'</td>
			<td align="center">'.$selisih_minus .'</td>
			<td align="right">'.$button_create.'</td>
		</tr>
		'; 
	}
	
	$paging = $kgPagerOBJ ->showPaging();	
	$view .='<tr><td colspan="11">'.$paging.'</td></tr>';
	$view .='</table>'."\n";
	return $view;
}

function form_ptk_create($id,$jumlah){

	my_set_file_js(
		array(
			'components/system/jquery/jquery.formatCurrency-1.4.0.js', 
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js', 
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  			
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);


	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
	');	
	
	$view = form_header( "ptk" , "ptk"  ); 
 	$view .= form_field_display( 'PTK'.date('Y').get_nomor_baru()    , "Nomor PTK");

	$tanggal_exists = date('d-m-Y');	
	$tanggal = array(
		'value' => isset($_POST['tanggal']) ? $_POST['tanggal'] : $tanggal_exists,
		'name' => 'tanggal', 
		'type'=>'text', 
	);
	$form_tanggal= form_calendar($tanggal); 
	$view .= form_field_display( $form_tanggal , 'Tgl. Dikeluarkan'  ) ;

	$jabatans = my_get_data_by_id('jabatan','jabatan_id',$id );
	$nomor_posisi=$jabatans['nomor_posisi']."/".$jabatans['nama_jabatan'] ;
	$view .= form_field_display( $nomor_posisi   , "No. Pos./Jabatan"   );

	$kebutuhan = array(
		'name'=>'kebutuhan',
		'value'=>( isset($_POST['kebutuhan']) ? $_POST['kebutuhan'] : $jumlah ),
		'id'=>'kebutuhan',
		'size'=>'3'
	);
	$form_field_kebutuhan = form_dynamic($kebutuhan); 	
	$view .= form_field_display( $form_field_kebutuhan.' orang'  , "Kebutuhan"  );
	
	$tanggal_exists = date('d-m-Y');	
	$tanggal = array(
		'value' => isset($_POST['mulai_berlaku']) ? $_POST['mulai_berlaku'] : $tanggal_exists,
		'name' => 'mulai_berlaku', 
		'type'=>'text', 
	);
	$form_tanggal= form_calendar($tanggal); 
	$view .= form_field_display( $form_tanggal , 'Mulai Berlaku'  ) ;	
	
	$tanggal_exists = date('d-m-Y');	
	$tanggal = array(
		'value' => isset($_POST['jadwal_kebutuhan']) ? $_POST['jadwal_kebutuhan'] : $tanggal_exists,
		'name' => 'jadwal_kebutuhan', 
		'type'=>'text', 
	);
	$form_tanggal= form_calendar($tanggal); 
	$view .= form_field_display( $form_tanggal , 'Jadwal Kebutuhan'  ) ;		

	$pengalaman_kerja = array(
		'name'=>'pengalaman_kerja',
		'value'=>( isset($_POST['pengalaman_kerja']) ? $_POST['pengalaman_kerja'] : '0' ),
		'id'=>'pengalaman_kerja',
		'size'=>'3'
	);
	$form_field_pengalaman_kerja = form_dynamic($pengalaman_kerja); 	
	$view .= form_field_display( $form_field_pengalaman_kerja.' tahun'  , "Pengalaman Kerja"  );	
	
	$peng_bahasa = array(
		'name'=>'peng_bahasa',
		'value'=>( isset($_POST['peng_bahasa']) ? $_POST['peng_bahasa'] : '' ),
		'id'=>'peng_bahasa',
		'size'=>'40'
	);
	$form_field_peng_bahasa = form_dynamic($peng_bahasa); 	
	$view .= form_field_display( $form_field_peng_bahasa  , "Peng. Bahasa"  );	
	
	$bakat = array(
		'name'=>'bakat',
		'value'=>( isset($_POST['bakat']) ? $_POST['bakat'] : '' ),
		'id'=>'bakat',
		'size'=>'40'
	);
	$form_field_bakat = form_dynamic($bakat); 	
	$view .= form_field_display( $form_field_bakat  , "Bakat/Minat"  );	

	$laki_laki = array(
		'name'=>'laki_laki',
		'value'=>( isset($_POST['laki_laki']) ? $_POST['laki_laki'] : '0' ),
		'id'=>'laki_laki',
		'size'=>'3'
	);
	$form_field = form_dynamic($laki_laki); 	
	$view .= form_field_display( $form_field.' orang'  , "Laki Laki"  );

	$wanita = array(
		'name'=>'wanita',
		'value'=>( isset($_POST['wanita']) ? $_POST['wanita'] : '0' ),
		'id'=>'wanita',
		'size'=>'3'
	);
	$form_field = form_dynamic($wanita); 	
	$view .= form_field_display( $form_field.' orang'  , "Wanita"  );

	$umur = array(
		'name'=>'umur',
		'value'=>( isset($_POST['umur']) ? $_POST['umur'] : ''),
		'id'=>'umur',
		'size'=>'20'
	);
	$form_field = form_dynamic($umur); 	
	$view .= form_field_display( $form_field  , "Umur"  );
	
	$tinggi = array(
		'name'=>'tinggi',
		'value'=>( isset($_POST['tinggi']) ? $_POST['tinggi'] : '' ),
		'id'=>'tinggi',
		'size'=>'20'
	);
	$form_field = form_dynamic($tinggi); 	
	$view .= form_field_display( $form_field  , "Tinggi"  );

	$berat = array(
		'name'=>'berat',
		'value'=>( isset($_POST['berat']) ? $_POST['berat'] : '' ),
		'id'=>'berat',
		'size'=>'20'
	);
	$form_field = form_dynamic($berat); 	
	$view .= form_field_display( $form_field  , "Berat"  );

	$contoh_tipe_pegawai = array(
		'name'=>'contoh_tipe_pegawai',
		'value'=>( isset($_POST['contoh_tipe_pegawai']) ? $_POST['contoh_tipe_pegawai'] : '' ),
		'id'=>'contoh_tipe_pegawai',
		'cols'=>'45',
		'rows'=>'2'
	);
	$form_field = form_textarea($contoh_tipe_pegawai );
	$view .= form_field_display( $form_field , "Contoh Tipe Pegawai"   );

	$saran = array(
		'name'=>'saran',
		'value'=>( isset($_POST['saran']) ? $_POST['saran'] : '' ),
		'id'=>'saran',
		'cols'=>'45',
		'rows'=>'2'
	);
	$form_field = form_textarea($saran );
	$view .= form_field_display( $form_field , "Saran"   );
	
	$tanggal_exists = date('d-m-Y');	
	$tanggal = array(
		'value' => isset($_POST['dibuat_tanggal']) ? $_POST['dibuat_tanggal'] : $tanggal_exists,
		'name' => 'dibuat_tanggal', 
		'type'=>'text', 
	);
	$form_tanggal= form_calendar($tanggal); 
	$view .= form_field_display( $form_tanggal , 'Dibuat Tanggal'  ) ;

	$karyawan_data = loaddata_karyawan(0);

	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>(isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : $karyawan_data['nik'].'/'.$karyawan_data['nama']  ),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'60',
		'onchange'=>'javascript:checkName(this.value)'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan  , "Dibuat Oleh (NIK/Nama Karyawan)"  );	

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$back = array(
		'value' => ' Kembali ',
		'name' => 'back', 
		'type'=>'button',
		'onclick'=>'location.href=\'index.php?com=kebutuhan\'',
		'class'=>'main_button'
	);
	$form_back = form_dynamic($back); 
	
	$view .= form_field_display( $form_submit .' '. $form_back , "&nbsp;" ,  "" );
	$view .= form_footer( );
	
	return $view;

}

function submit_ptk_create($id=0){
//	$id= int $id;
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);

	list($dd,$mm,$yyyy) = explode( "-" , $_POST['tanggal']);
	list($dd1,$mm1,$yyyy1) = explode( "-" , $_POST['mulai_berlaku']);
	list($dd2,$mm2,$yyyy2) = explode( "-" , $_POST['jadwal_kebutuhan']);
	list($dd3,$mm3,$yyyy3) = explode( "-" , $_POST['dibuat_tanggal']);
	$datas = array(
		'nomor'				=>	my_type_data_str( 'PTK'.date('Y').get_nomor_baru() 	),
		'tanggal'  			=> 	my_type_data_str( $yyyy.'-'.$mm.'-'.$dd),		 	 	 	
		'jabatan_id'		=>  my_type_data_int($id ),		
		'kebutuhan'			=> 	my_type_data_str( $_POST['kebutuhan'] ),			 
		'mulai_berlaku'		=> 	my_type_data_str( $yyyy1.'-'.$mm1.'-'.$dd1),		
		'jadwal_kebutuhan'	=> 	my_type_data_str( $yyyy2.'-'.$mm2.'-'.$dd2),		
	//	'pendidikan_id'		=> 	my_type_data_str( $_POST['catatan'] ),	
		'pengalaman_kerja'	=> 	my_type_data_str( $_POST['pengalaman_kerja'] ),	
		'peng_bahasa'		=> 	my_type_data_str( $_POST['peng_bahasa'] ),	
		'bakat'				=> 	my_type_data_str( $_POST['bakat'] ),	
		'laki_laki'			=> 	my_type_data_str( $_POST['laki_laki'] ),	
		'wanita'			=> 	my_type_data_str( $_POST['wanita'] ),	
		'umur'				=> 	my_type_data_str( $_POST['umur'] ),	
		'tinggi'			=> 	my_type_data_str( $_POST['tinggi'] ),	
		'berat'				=> 	my_type_data_str( $_POST['berat'] ),	
		'contoh_tipe_pegawai'=> my_type_data_str( $_POST['contoh_tipe_pegawai'] ),	
		'saran'				=> 	my_type_data_str( $_POST['saran'] ),	
		'dibuat_tanggal'	=> 	my_type_data_str( $yyyy3.'-'.$mm3.'-'.$dd3),		
		'dibuat_oleh'		=> 	my_type_data_int($karyawan_id),
	);
	
	return my_insert_record( 'ptk' , $datas );
}

function ptk_validate($id, $jumlah){
	$errsubmit = false;
	$err = array();
	if(!is_numeric($_POST['kebutuhan']) ){
		$errsubmit = true;
		$err[] = "Kebutuhan harus angka" ;
	}elseif( $_POST['kebutuhan'] > $jumlah){
		$errsubmit = true;
		$err[] = "Kebutuhan maksimum ". $jumlah ;
	}
	if(!is_numeric($_POST['pengalaman_kerja']) ){
		$errsubmit = true;
		$err[] = "Pengalaman Kerja harus angka" ;
	}	
	if(!is_numeric($_POST['laki_laki']) ){
		$errsubmit = true;
		$err[] = "Laki_laki harus angka" ;
	}
	if(!is_numeric($_POST['wanita']) ){
		$errsubmit = true;
		$err[] = "Wanita harus angka" ;
	}
	if( trim($_POST['nama_karyawan']) == '/' or trim($_POST['nama_karyawan']) == ''){
		$errsubmit = true;
		$err[] = "Dibuat oleh belum di isi";
	}else{
		$karyawan_id= check_karyawan_by_nik_nama($_POST['nama_karyawan']);
		if($karyawan_id == 0){
			$errsubmit = true;
			$err[] = "Nama karyawan tidak di temukan";	
		}	
	}	
	if( $errsubmit){
		return $err;
	}
	
	return false;
}

function get_nomor_baru(){
	$query = "SELECT MAX(nomor) AS mmax FROM ptk WHERE YEAR(tanggal) = YEAR(NOW())";
	$result = my_query($query);
	$row = my_fetch_array($result);
	$no=substr($row['mmax'],7,4)+1;
	return sprintf('%04d' , $no );
}

function get_jabatan_link_by_jabatan_id($jabatan_id){
	$jabatans = my_get_data_by_id('jabatan','jabatan_id' , $jabatan_id ); 
	$jabatan_nama=$jabatans['nama_jabatan'];
	$query = "SELECT b.nama_jabatan FROM jabatan_terkait_link a 
	INNER JOIN jabatan b ON a.jabatan_id=b.jabatan_id 
	WHERE a.parent_jabatan_id  =".$jabatan_id ;
	$result = my_query($query);
	while($ey = my_fetch_array($result)){
			$jabatan_nama.=",<br>".$ey['nama_jabatan'];
	}		
	return $jabatan_nama ;
}

function get_nomor_posisi_link_by_jabatan_id($jabatan_id){
	$jabatans = loaddata_jabatan( $jabatan_id ); 
	$jabatan_nama=$jabatans['nomor_posisi'];
	$query = "SELECT b.nomor_posisi FROM jabatan_terkait_link a 
		INNER JOIN jabatan b ON a.jabatan_id=b.jabatan_id 
		WHERE a.parent_jabatan_id  =".$jabatan_id ;
	$result = my_query($query);
	while($ey = my_fetch_array($result)){
			$jabatan_nama.=",<br>".$ey['nomor_posisi'];
	}		
	return $jabatan_nama ;
}


function getproses_ptk($jabatan_id ){
	
	$query = "SELECT sum(kebutuhan) AS total_data FROM ptk
	WHERE jabatan_id = {$jabatan_id} "; 
	$result = my_query($query);
	$row = my_fetch_array($result);
	$avai_data=$row['total_data'] ;
	return $avai_data ;
}